//
//  HospitalNavController.m
//  HarmonizePub
//
//  Created by rzhy on 2016/10/27.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "HospitalNavController.h"
#import "Config.h"
#import <NJKWebViewProgressView.h>
#import <NJKWebViewProgress.h>
#import "MapNaviController.h"
//#import "FloorNaviController.h"
#import "WebViewController.h"

@interface HospitalNavController () <UIWebViewDelegate, NJKWebViewProgressDelegate>
{
    NSDictionary *_dataDict;
    NJKWebViewProgressView *_webViewProgressView;
    NJKWebViewProgress *_webViewProgress;
}
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIView *dockView;

@end

@implementation HospitalNavController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createProgress];
    NSURL *url = [URLTool webURLStringWithPathString:XHHospitalNav token:nil];
    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
    //[self createButton];
}


#pragma mark - create button
- (void)createButton {
//    NSArray *imageNameArray = @[@"MapNavigation.png", @"FloorNavigation.png"];
    NSArray *imageNameArray = @[@"FloorNavigation.png"];
    CGFloat buttonH = 40.0f;
    CGFloat buttonY = 0.0f;
    NSInteger count = imageNameArray.count;
    for (NSInteger i = 0; i < count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        UIImage *image = [UIImage imageNamed:imageNameArray[i]];
        [button setImage:image forState:UIControlStateNormal];
        CGFloat buttonW = image.size.width / (image.size.height / buttonH);
        CGFloat buttonX = XHScreenWidth / count * i + ((XHScreenWidth / count - buttonW) / 2);
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        if (i+1)
            [button addTarget:self action:@selector(floorNavigationEvent:) forControlEvents:UIControlEventTouchUpInside];
        else
            [button addTarget:self action:@selector(mapNavigationEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.dockView addSubview:button];
    }
}

#pragma mark - button click
#pragma mark 地图导航
- (IBAction)mapNavigationEvent:(UIButton *)sender {
    
    CGFloat latitude  = 35.870249;  // 纬度
    CGFloat longitude = 117.313872; // 经度
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/marker?location=%f,%f&coord_type=bd09ll&title=新泰市第三人民医院&content=新泰市第三人民医院&src=新泰第三医院",latitude,longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:urlString];
        [[UIApplication sharedApplication] openURL:url];
    }else{
        [ZYToast showBottomText:@"请安装百度地图"];
    }
//    MapNaviController *mapNavi = [[MapNaviController alloc] init];
//    [self.navigationController pushViewController:mapNavi animated:YES];
}

#pragma mark 楼层导航
- (IBAction)floorNavigationEvent:(UIButton *)sender {
//    FloorNaviController *floorNavi = [[FloorNaviController alloc] init];
//    [self.navigationController pushViewController:floorNavi animated:YES];
    WebViewController *web = [[WebViewController alloc] init];
    web.title = @"楼层导航";
    web.url = [URLTool webURLStringWithPathString:@"/ws/navigationsearch2" token:[UserDefaultsTool stringForKey:XHToken]];
    [self.navigationController pushViewController:web animated:YES];
}

#pragma mark - webView delegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie *cookie in [storage cookies])
        [storage deleteCookie:cookie];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if (!webView.userInteractionEnabled) webView.userInteractionEnabled = YES;
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if (navigationType == UIWebViewNavigationTypeLinkClicked)
        if ([[request.URL absoluteString] hasPrefix:@"http"]) {
            [[UIApplication sharedApplication] openURL:request.URL];
            return NO;
        }
    return YES;
}

#pragma mark - Progress
- (void)createProgress {
    _webViewProgress = [[NJKWebViewProgress alloc] init];
    _webView.delegate = _webViewProgress;
    _webViewProgress.webViewProxyDelegate = self;
    _webViewProgress.progressDelegate = self;
    
    CGRect navBounds = self.navigationController.navigationBar.bounds;
    CGRect barFrame = CGRectMake(0, navBounds.size.height - 2, navBounds.size.width, 2);
    _webViewProgressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _webViewProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [_webViewProgressView setProgress:0 animated:YES];
    [self.navigationController.navigationBar addSubview:_webViewProgressView];
}

#pragma mark - NJKWebViewProgress Delegate
- (void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress {
    [_webViewProgressView setProgress:progress animated:YES];
}

@end

@implementation NSURLRequest(ForSSL)

+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host {
    return YES;
}

@end
